package com.ym.b2b.buyer.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.buyer.service.EmCBuyerAddressService;
import com.ym.b2b.buyer.sqlbean.EmCBuyerAddress;
import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: BuyerAddressController
 * @Description: TODO
 * @Author tangxj
 * @Date 2014-12-29 下午4:26:26
 * @Modify
 * @CopyRight B2B小组
 */

@Controller
@RequestMapping("/buyerAddress")
public class BuyerAddressController extends BaseController {

    private Logger                 logger = Logger.getLogger(getClass());

    @Autowired
    private EmCBuyerAddressService emCBuyerAddressService;

    /**
     * @Title: searchPageBuyerAddress
     * @Description: 前台 商品搜索 列表界面
     * @Param @param EmCBuyerAddress
     * @Param @param page
     * @Param @param req
     * @Param @return
     * @Return String
     * @Throws
     */
    @RequestMapping("searchPageBuyerAddress.s")
    @ResponseBody
    public Result searchPageBuyerAddress(EmCBuyerAddress emCBuyerAddress, Page<EmCBuyerAddress> page,
                                         HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            emCBuyerAddress.setBuyerCode(tokenUser.getBuyerCode());
            result = emCBuyerAddressService.searchPageBuyerAddress(emCBuyerAddress);
            result.putData("svo", emCBuyerAddress);
            result.putData("info", req.getAttribute("info"));
            return result;
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 备份
     * 
     * @Title: searchBuyerAddressTemp
     */
    @RequestMapping("searchBuyerAddressTemp.s")
    public ModelAndView searchBuyerAddressTemp(EmCBuyerAddress emCBuyerAddress, Page<EmCBuyerAddress> page,
                                               HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            if (tokenUser == null) {
                m.setViewName("website/login");
                return m;
            }

            emCBuyerAddress.setBuyerCode(tokenUser.getBuyerCode());
            page.setPageable("true");
            page.setPageUrl(req);
            Result result = new Result();

            result = emCBuyerAddressService.searchPageBuyerAddress(emCBuyerAddress);
            result.putData("svo", emCBuyerAddress);
            result.putData("info", req.getAttribute("info"));
            m.addObject("result", result);

            m.setViewName("website/trade/s_BuyerAddresslist");

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.addObject("msg", e.getMessage());
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    /**
     * @Title: addBuyerAddress
     * @Description: TODO
     * @Param @param EmCBuyerAddress
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping("addBuyerAddress.s")
    @ResponseBody
    public Result addBuyerAddress(EmCBuyerAddress emCBuyerAddress, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "~@^_^@~ 请先登录采购商!");
                return result;
            }
            emCBuyerAddress.setBuyerCode(token.getBuyerCode());
            emCBuyerAddress.setUpdateUser(token.getUserName());
            result = emCBuyerAddressService.addBuyerAddress(emCBuyerAddress);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: updateBuyerAddress
     * @Description: TODO
     * @Param @param EmCBuyerAddress
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping("editBuyerAddress.s")
    @ResponseBody
    public Result editBuyerAddress(EmCBuyerAddress emCBuyerAddress, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "~@^_^@~ 请先登录采购商!");
                return result;
            }
            result = emCBuyerAddressService.updateBuyerAddress(emCBuyerAddress);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 删除购物车信息
     * 
     * @Title: deleteBuyerAddress
     * @Description: TODO
     * @Param @param EmCBuyerAddress
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping("deleteBuyerAddress.s")
    @ResponseBody
    public Result deleteBuyerAddress(EmCBuyerAddress emCBuyerAddress, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登录采购商!");
                return result;
            }
            result = emCBuyerAddressService.deleteBuyerAddress(emCBuyerAddress);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
}
